﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;

using DotNet.Core.Entity;
using DotNet.Core.Service;
using DotNet.Helper;
using DotNet.Utility;
using System.Web.Mvc;
using DotNet.Extensions;
using DotNet.JCore.Service;
using DotNet.JCore.Entity;
using DotNet.Auth.Utility;
namespace DotNet.Web.Controllers
{
    public class ManualRegController : BaseController
    {
        private ManualRegService service = new ManualRegService();
        public ActionResult Index()
        {
            var currentUser = AuthHelper.GetSessionUser();
            if (!currentUser.IsAdmin)
            {
                ViewBag.pointId = CurrentEmployee.PointId;
            }
            return View();
        }

        public ActionResult Grid(string name, string Station, string CheckStatusName, string PointId, string startDate, string endDate)
        {
            var list = service.GetPageList(PageInfo(),name,Station, CheckStatusName, PointId, startDate.ToDateTimeOrNull().ToString(), endDate.ToDateTimeOrNull().ToString());
            return View(list);
        }

        public ActionResult Create()
        {
            MarkCreate();
            return EditCore(new ManualReg
            {
                Id = StringHelper.Guid()
            });
        }

        public ActionResult Edit(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return EditCore(entity);
        }

        private ActionResult EditCore(ManualReg entity)
        {
            return View("Edit", entity);
        }

        [HttpPost]
        [ValidateAntiForgeryToken]
        /// 0：正常1：违规
        public ActionResult Save(ManualReg entity)
        {
            if (entity.CheckStatusName.Trim() == "正常")
            {
                entity.OffenceId = null;
                entity.OffenceName = null;
            }
            AuthHelper.SetJCUserInfo(entity, IsCreate);
            var result = service.Save(entity, IsCreate);
            return Json(result);
        }

        [HttpPost]
        public ActionResult Delete(string id)
        {
            service.Delete(id.SplitToArray());
            return Json(BoolMessage.True);
        }

        public ActionResult Details(string id)
        {
            var entity = service.Get(id);
            if (entity == null) return NotFound(id);
            return View(entity);
        }

        public ActionResult Export(string name, string Station, string CheckStatusName, string PointId, string startDate, string endDate)
        {
            return Export(service.GetList(name, Station, CheckStatusName, PointId, startDate.ToDateTimeOrNull().ToString(), endDate.ToDateTimeOrNull().ToString()));
        }

        private ActionResult NotFound(string id)
        {
            return NotFound("获取 人员信息 错误", $"无法找到 Id = {id} 的人员信息");
        }
    }
}